// Functions associated with the SidebarPlaced class
//-----------------

//-----------------
// Includes libraries and other files
#include "SidebarPlaced.h"
//-----------------

//-----------------
//// CONSTRUCTOR - SidebarPlaced
SidebarPlaced::SidebarPlaced( const Window& window, UserInterface* p_ui, const int& height, const int& side)
	: Sidebar( window, p_ui, height, side)
{}

//// DESTRUCTOR - ~SidebarPlaced
SidebarPlaced::~SidebarPlaced() {}
//-----------------

//-----------------
//// FUNCTION - mouseover_asset
// return true if an item in one of the lists in being cursored over
bool SidebarPlaced::mouseover_asset( const int& x, const int& y) {
	// Pre-conditions :- none
	assert( true);

	int list_x( 0), list_y( 0), counter( 0);
	get_asset_list_coordinates( list_x, list_y);

	for( int i( 0); i < placed_objects_.size(); ++i) {
		for( int j( 0); j < placed_objects_[i].size(); ++j) {
			// ##### NOTE #####
			// The stuff inside this for loop is a buch of bull atm.
			// I'm currently just trying to get the program to run, so this NEEDS re-doing.
			// Problem :- It currently doesn't output the objects in alphabetical order.
			if(( x >= list_x && x <= (list_x + (placed_objects_[i][j].get_object_name().length()*CHAR_WIDTH))) &&
				( y >= list_y  && y <= (list_y  + CHAR_HEIGHT)) ) {
					set_asset_mouseover( counter);
					return true;
			}

			if( placed_objects_[i][j].get_object_type() == get_active_button())
				list_y += (CHAR_HEIGHT+5);
			// ##### NOTE #####
		}
	}

	// if none of the buttons are under the mouse then return false
	set_asset_mouseover( -1);
	return false;
}

//// FUNCTION - add_asset
// add the asset to the correct array, based on its object type
void SidebarPlaced::add_asset( const int& x, const int& z, const ModelAsset& m) {
	// Pre-conditions :- the asset type must be valid
	assert( m.get_asset_type() >= BUTTON_FLOOR && m.get_asset_type() < BUTTON_ALL);

	if( m.get_asset_type() != BUTTON_FLOOR) {
		// if the object is the player's start position then remove previous instances of it first
		if( m.get_file_name() == WAYPOINT_START) {
			for( int i( 0); i < placed_objects_.size(); ++i) {
				bool found( false);
				for( int j( 0); j < placed_objects_[i].size(); ++j) {
					if( placed_objects_[i][j].get_file_name() == WAYPOINT_START) {
						// use a blank variable to replace the previous waypoint
						ModelAsset empty;
						placed_objects_[i][j] = empty;
						found = true;
						break;
					}
				}
				// if the waypoint has been found then exit the loop
				if( found)
					break;
			}
		}

		// insert the new object into the array
		placed_objects_[x][z] = m;
		placed_objects_[x][z].set_position( x*GRID_SIZE, 0.0f, z*GRID_SIZE);
	}
	else {
		placed_floor_[x][z] = m;
		placed_floor_[x][z].set_position( x*GRID_SIZE, 0.0f, z*GRID_SIZE);
	}
}

//// FUNCTION - render
// output the sidebar
void SidebarPlaced::render( LPDIRECT3DDEVICE9 device, const Window& window) {
	// Pre-conditions :- the device must exist
	assert( device != nullptr);

	// render the sidebar's background as well
	Sidebar::render( device, window);

	// render the lists of assets
	int x( 0), y( 0), limit( get_y() + get_height() - CHAR_HEIGHT - 4);
	get_asset_list_coordinates( x, y);

	// if the active button is the floor then just output the placed_floor_ array
	if( get_active_button() == BUTTON_FLOOR) {
		for( int i( 0); i < placed_floor_.size(); ++i)
			for( int j( 0); j < placed_floor_.size(); ++j)
				// ensure that the y position is not too low and that the array slot has something in it
				if( y < limit && placed_floor_[i][j].get_file_name() != "") {
					p_ui_->render_text( placed_floor_[i][j].get_object_name(), x, y, 255, 255, 255, 255);

					// move down in preparation for the next asset being written
					y += (CHAR_HEIGHT+5);
				}
	}
	// otherwise run through the placed_objects_ array testing each of their object types to see if they match the active button
	else {
		for( int i( 0); i < placed_objects_.size(); ++i)
			for( int j( 0); j < placed_objects_[i].size(); ++j)
				if( placed_objects_[i][j].get_object_type() == get_active_button()) {
					// ensure that the y position is not too low
					if( y < limit)
						p_ui_->render_text( placed_objects_[i][j].get_object_name(), x, y, 255, 255, 255, 255);

					// move down in preparation for the next asset being written
					y += (CHAR_HEIGHT+5);
				}
	}
}

//// FUNCTION - reset
// clear all the user-created data in preparation of a new module
void SidebarPlaced::reset() {
	// Pre-conditions :- none
	assert( true);

	Sidebar::reset();

	PlacedObject temp;
	for( int i( 0); i < placed_objects_.size(); ++i) {
		for( int j( 0); j < placed_objects_.size(); ++j) {
			placed_objects_[i][j] = temp;
			placed_floor_[i][j] = temp;
		}
	}
}